﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.RawDict;
using StoragePeriod.DALFactory;
using StoragePeriod.Model.RawDict;
using System.Data;

namespace StoragePeriod.BLL.RawDict
{
    /// <summary>
    /// 原料操作
    /// </summary>
    public class T_RawMaterial
    {
        I_RawMaterial dal = new DataAccess<I_RawMaterial>().CreateInstance("RawDict.D_RawMaterial");

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 根据查询条件获取原料数据列表
        /// </summary>
        /// <param name="eRawMaterial">查询条件</param>
        /// <returns>返回符合要求的原料数据列表</returns>
        public List<E_RawMaterial> GetRawListByWhere(E_RawMaterial eRawMaterial)
        {
            return dal.GetRawListByWhere(eRawMaterial);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(E_RawMaterial model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_RawMaterial model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_RawMaterial GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<E_RawMaterial> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<E_RawMaterial> DataTableToList(DataTable dt)
        {
            List<E_RawMaterial> modelList = new List<E_RawMaterial>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                E_RawMaterial model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new E_RawMaterial();
                    if (dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    model.RAWName = dt.Rows[n]["RAWName"].ToString();
                    if (dt.Rows[n]["RAWTID"].ToString() != "")
                    {
                        model.RAWTID = int.Parse(dt.Rows[n]["RAWTID"].ToString());
                    }
                    if (dt.Rows[n]["RAWID"].ToString() != "")
                    {
                        model.RAWID = int.Parse(dt.Rows[n]["RAWID"].ToString());
                    }
                    if (dt.Rows[n]["RAWIDS"].ToString() != "")
                    {
                        model.RAWIDS = int.Parse(dt.Rows[n]["RAWIDS"].ToString());
                    }
                    model.RAWSid = dt.Rows[n]["RAWSid"].ToString();
                    model.RAWWid = dt.Rows[n]["RAWWid"].ToString();
                    model.InDBid = dt.Rows[n]["InDBid"].ToString();
                    model.Back = dt.Rows[n]["Back"].ToString();
                    model.temp1 = dt.Rows[n]["temp1"].ToString();
                    model.temp2 = dt.Rows[n]["temp2"].ToString();
                    if (dt.Rows[n]["RAWZS"].ToString() != "")
                    {
                        model.RAWZS = int.Parse(dt.Rows[n]["RAWZS"].ToString());
                    }
                    model.SafetyTips = dt.Rows[n]["SafetyTips"].ToString();
                    if (dt.Rows[n]["PeriodTypeID"].ToString() != "")
                    {
                        model.PeriodTypeID = int.Parse(dt.Rows[n]["PeriodTypeID"].ToString());
                    }
                    if (dt.Rows[n]["idkey"].ToString() != "")
                    {
                        model.idkey = new Guid(dt.Rows[n]["idkey"].ToString());
                    }
                    if (dt.Rows[n]["PeriodTypeStorageID"].ToString() != "")
                    {
                        model.PeriodTypeStorageID = int.Parse(dt.Rows[n]["PeriodTypeStorageID"].ToString());
                    }
                    if (dt.Rows[n]["PeriodTypeKnowledgeID"].ToString() != "")
                    {
                        model.PeriodTypeKnowledgeID = int.Parse(dt.Rows[n]["PeriodTypeKnowledgeID"].ToString());
                    }
                    if (dt.Rows[n]["PeriodTypeStorKnowledgeID"].ToString() != "")
                    {
                        model.PeriodTypeStorKnowledgeID = int.Parse(dt.Rows[n]["PeriodTypeStorKnowledgeID"].ToString());
                    }
                    if (dt.Rows[n]["UsefulPart"].ToString() != "")
                    {
                        model.UsefulPart = decimal.Parse(dt.Rows[n]["UsefulPart"].ToString());
                    }
                    if (dt.Rows[n]["IsEdit"].ToString() != "")
                    {
                        model.IsEdit = int.Parse(dt.Rows[n]["IsEdit"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
    }
}
